#!/usr/bin/env python
import os
import sys
import unittest

# Expand system path with tested library source.
libnest_path = os.path.abspath(__file__).split(os.sep)
libnest_path = os.sep.join(
    libnest_path[0:libnest_path.index('libnest') + 1] + ['src', 'python']
)
sys.path = [libnest_path] + sys.path

from nest import Nest
from nest.path import Path
from nest.tree.exception import MapError
from nest.path.exception import (PathError, MissingParser)
from nest.tree.abstract import AbstractTree
from nest.path.parser import PathParser
from nest.path.iterator import PathIterator
from nest.path.parser.stroke import StrokeParser
from nest.tree.data import DataTree
from nest.path.parsed import ParsedPath

class MockTree(AbstractTree):
    pass


class TestNestPath(unittest.TestCase):

    def setUp(self):
        pass

    def test_path_iterator(self):
        path = 'test/path'
        tree = Nest(MockTree, StrokeParser).sample()
        path_iter = Path.resolve_path(tree, path)
        self.assertTrue(len(list(path_iter)) == len(list(path_iter)))

    def test_path_list(self):
        path = 'test/path'
        tree = Nest(MockTree, StrokeParser).sample()
        path_iter = Path.resolve_path(tree, path)
        path_list = ParsedPath(path_iter)
        self.assertTrue(isinstance(path_list, Path))
        self.assertEquals(len(path_list), 2)

    def test_paths_enumeration(self):
        tree = Nest(DataTree, StrokeParser).sample().cover(
            {'a': {'nested' : {
                'path': 'value',
                'path2': 'another value',
            }}}
        )
        expected_paths = [
            'a', 'a/nested', 'a/nested/path2', 'a/nested/path']        
        self.assertEquals(expected_paths, list(tree.paths(join_paths=True)))
        # Compare string representation of path lists.
        expected_paths = [
            'a',
            ['a', 'nested'], 
            ['a', 'nested', 'path2'], 
            ['a', 'nested', 'path']]
        actual_paths = tree.paths()
        for index, expected_path in enumerate(expected_paths):
            self.assertEquals(str(expected_path), str(actual_paths[index]))

        
if __name__ == '__main__':
        unittest.main(defaultTest='TestNestPath.test_paths_enumeration')
